package yun.web.activerecord.dialect;

import cn.hutool.core.io.resource.Resource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.yaml.snakeyaml.Yaml;
import yun.web.activerecord.ActiveRecordException;
import yun.web.activerecord.Db;
import yun.web.activerecord.Model;
import yun.web.activerecord.Record;
import yun.web.activerecord.dialect.mysql.MysqlARP;
import yun.web.activerecord.dialect.pg.PGARP;

import java.util.List;
import java.util.stream.Collectors;

public class ARP {
    public static void init(JSONArray dbConfigList) throws Exception {
        List<JSONObject> enabledDbConfig = dbConfigList.toJavaList(JSONObject.class).stream()
                .filter(it -> it.getBoolean("enable"))
                .collect(Collectors.toList());

        int arpIdListSize = enabledDbConfig.stream().map(it -> {
            String arp_id = it.getString("id");
            if (arp_id == null || "".equals(arp_id)) {
                throw new ActiveRecordException("数据源id不能为空");
            }
            return arp_id;
        }).collect(Collectors.toSet()).size();

        if (arpIdListSize != enabledDbConfig.size()) {
            throw new ActiveRecordException("存在重复的 arp_id!");
        }

        for (JSONObject it : enabledDbConfig) {
            String url = it.getString("url");
            if (url.startsWith("jdbc:postgresql")) {
                PGARP.init(it);
            } else {
                MysqlARP.init(it);
            }
            System.out.println(Db.use(it.getString("id")).queryInt("select 1"));
        }
    }


    public static void generateModel(Resource configResource) throws Exception {
        String configString = configResource.readUtf8Str();
        JSONObject yaml = new Yaml().loadAs(configString, JSONObject.class);
        JSONArray dbConfigList = JSON.parseArray(yaml.getJSONArray("datasource").toJSONString());
        generate(dbConfigList);
    }


    public static void generate(JSONArray dbConfigList) throws Exception {
        for (JSONObject it : dbConfigList.toJavaList(JSONObject.class)) {
            String url = it.getString("url");
            if (url.startsWith("jdbc:postgresql")) {
                PGARP.generateModel(it);
            } else {
                MysqlARP.generateModel(it);
            }
        }
    }

    public static void build(Record record, Object... kvs) {
        if (kvs.length % 2 != 0)
            throw new ActiveRecordException("yun.web.activerecord.model.build(java.lang.Object...)参数个数必须为偶数");
        for (int i = 0; i < kvs.length; i += 2) {
            String k = (String) kvs[i];
            Object v = kvs[i + 1];
            record.set(k, v);
        }
    }

    public static <T extends Model<T>> void build(T model, Object... kvs) {
        if (kvs.length % 2 != 0)
            throw new ActiveRecordException("yun.web.activerecord.model.build(java.lang.Object...)参数个数必须为偶数");
        for (int i = 0; i < kvs.length; i += 2) {
            String k = (String) kvs[i];
            Object v = kvs[i + 1];
            model.set(k, v);
        }
    }


}
